package com.car.serve.service.impl;



import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.car.serve.Param.addCarTypeParam;
import com.car.serve.entity.CarType;
import com.car.serve.exception.ServeException;
import com.car.serve.form.PageCarTypeForm;
import com.car.serve.mapper.CarTypeMapper;
import com.car.serve.service.CarTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 董旭
 * @since 2024-08-28
 */
@Service
public class CarTypeServiceImpl extends ServiceImpl<CarTypeMapper, CarType> implements CarTypeService {
    @Resource
    private CarTypeMapper carTypeMapper;
    @Override
    public void addCarType(addCarTypeParam param) throws ServeException {
        QueryWrapper<CarType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("car_type_name",param.getCarTypeName());
        CarType carType = carTypeMapper.selectOne(queryWrapper);
        if (carType!=null)throw new ServeException(100,"车辆类型已存在");
        //新增车辆类型
        CarType carType1 = BeanUtil.toBean(param, CarType.class);
        carType1.setCarTypeName(param.getCarTypeName());
        carTypeMapper.insert(carType1);
    }

    @Override
    public Page<CarType> findCarTypePage(PageCarTypeForm form)throws ServeException {
        Page<CarType> page = new Page<>(form.getPageNum() == null ? 1 : form.getPageNum(), form.getPageSize() == null ? 6 : form.getPageSize());
        QueryWrapper<CarType> wrapper = new QueryWrapper<>();
        //判断类别名是否为空
        if (ObjectUtil.isNotEmpty(form.getCarTypeName())){
            wrapper.like("car_type_name",form.getCarTypeName());
        }
        Page<CarType> carTypePage = carTypeMapper.selectPage(page, wrapper);
        return carTypePage;
    }

    @Override
    public List<CarType> getMyCarType() {
        List<CarType> carTypes = carTypeMapper.selectList(null);
        return carTypes;
    }
}
